﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace VisualMove.Data
{
    public class Move
    {
        public Move(StorageFolder oFolder)
        {
            MoveFolder = oFolder;
        }

        public async Task<Box> FindBox(QRCodeWrapper oQRCode)
        {
            CurrentBox = Boxes.FirstOrDefault(oBox => oBox.QRCode == oQRCode);
            if (CurrentBox == null)
            {
                CurrentBox = new Box(this, oQRCode);
                await MoveFolder.CreateFolderAsync(CurrentBox.ImageFolder);
                Boxes.Add(CurrentBox);
            }

            return CurrentBox;
        }

        public string Name
        {
            get
            {
                return MoveFolder.Name;
            }
        }

        public StorageFolder MoveFolder
        {
            get;
            private set;
        }

        public Box CurrentBox;

        public Collection<Box> Boxes = new Collection<Box>();

        public Collection<Box> SortByLabel(string sLabel)
        {
            Collection<Box> oBoxes = new Collection<Box>();

            for (int i = 0; i < Boxes.Count(); i++)
            {
                if(Boxes[i].QRCode.QRCodeLabel == sLabel)
                {
                    oBoxes.Add(Boxes[i]);
                }
            }

            return oBoxes;
        }

        public async void LoadFolders()
        {
            CurrentBox = null;
            Boxes.Clear();
            IReadOnlyList<StorageFolder> oFolders = await MoveFolder.GetFoldersAsync();

            foreach (StorageFolder oFolder in oFolders)
            {
                //If a QR code has no images associated, we kill the folder
                if ((await oFolder.GetFilesAsync()).Count == 0)
                    await oFolder.DeleteAsync();
                else
                    Boxes.Add(new Box(this, new QRCodeWrapper(oFolder)));
            }
        }

        public async Task DeleteLabel(string sLabel)
        {
            int iBoxes = Boxes.Count();
            for (int i = 0; i < iBoxes; i++)
            {
                if (Boxes[i].QRCode.QRCodeLabel == sLabel)
                {
                    await StorageFolder.GetFolderFromPathAsync(Boxes[i].ImageFolder).GetResults().DeleteAsync();
                }
            }
            CurrentBox = null;
        }

        public async Task DeleteCurrentBox()
        {
            if (CurrentBox != null)
            {
                await StorageFolder.GetFolderFromPathAsync(CurrentBox.ImageFolder).GetResults().DeleteAsync();
                CurrentBox = null;
            }
        }

        public async Task Clear()
        {
            int iBoxes = Boxes.Count();
            for (int i = 0; i < iBoxes; i++)
            {
                await StorageFolder.GetFolderFromPathAsync(Boxes[i].ImageFolder).GetResults().DeleteAsync();
            }
            CurrentBox = null;
        }
        
        public override string ToString()
        {
            return Name;
        }
    }
}